package com.lee.dutylower.view.scheduling;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.lee.dutylower.MyApplication;
import com.lee.dutylower.R;
import com.lee.dutylower.db.greenEntity.PostRecord;
import com.lee.dutylower.db.greenEntity.Sentry;
import com.lee.dutylower.db.greenEntity.Soldier;
import com.lee.dutylower.db.greenEntity.Times;
import com.lee.dutylower.db.greendao.PostRecordDao;
import com.lee.dutylower.db.greendao.SentryDao;
import com.lee.dutylower.db.greendao.TimesDao;
import com.lee.dutylower.view.wiget.DividerItemDecoration;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lijie on 2017/6/8.
 */

public class OneDayScheduleFragment extends Fragment {
    private TimesDao timeDao;
    private PostRecordDao postRecordDao;
    private SentryDao sentryDao;
    private ListView lvData;

    private RecyclerView rvHeader;
    private LinearLayout llBody;

    private List<Times> values;
    private MyAdapter myAdapter;

    private List<Sentry> sentryDBs;
    private MyRVAdapter myRVAdapter;

    private List<MyRVAdapter4DB> adapter4DBs;

    private Integer dayofweek = 1;

    private int width = 117;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        timeDao = MyApplication.getApplication().getDaoSession().getTimesDao();
        sentryDao = MyApplication.getApplication().getDaoSession().getSentryDao();
        postRecordDao = MyApplication.getApplication().getDaoSession().getPostRecordDao();

        adapter4DBs = new ArrayList<>();

        sentryDBs = sentryDao.queryBuilder().build().list();
        values = timeDao.queryBuilder().build().list();

        View v = View.inflate(getActivity(),R.layout.fragment_one_schedule,null);
        lvData = (ListView) v.findViewById(R.id.lv_schedule_times);
        rvHeader = (RecyclerView) v.findViewById(R.id.rv_head);
        llBody = (LinearLayout) v.findViewById(R.id.ll_body);

        myAdapter = new MyAdapter();
        lvData.setAdapter(myAdapter);

        myRVAdapter = new MyRVAdapter();
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(),1, LinearLayoutManager.HORIZONTAL,false);
        rvHeader.addItemDecoration(new DividerItemDecoration(getActivity(),DividerItemDecoration.VERTICAL_LIST));
        rvHeader.addItemDecoration(new DividerItemDecoration(getActivity(),DividerItemDecoration.HORIZONTAL_LIST));
        rvHeader.setLayoutManager(gridLayoutManager);
        rvHeader.setAdapter(myRVAdapter);

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

        for (int i = 0; i < values.size(); i++) {
            List<Soldier> soldierDBs = new ArrayList<>();
            for (int j = 0; j < sentryDBs.size(); j++) {
                PostRecord pr = postRecordDao.queryBuilder()
                        .where(PostRecordDao.Properties.SentryID.eq(sentryDBs.get(j).getSentry_id()),
                                PostRecordDao.Properties.TimesID.eq(values.get(i).getTimeID()),
                                PostRecordDao.Properties.Dayofweek.eq(dayofweek))
                        .build().unique();
                if (pr != null){
                    soldierDBs.add(pr.getSoldier());
                }else {
                    if (sentryDBs.get(j).getSentry_id()==6l && values.get(i).getTimeID()==8l){
                        Soldier s = new Soldier();
                        s.setSName("昨夜302");
                        soldierDBs.add(s);
                    }else {
                        soldierDBs.add(null);
                    }
                }
            }
            MyRVAdapter4DB myRVAdapter4DB = new MyRVAdapter4DB(soldierDBs);
            adapter4DBs.add(myRVAdapter4DB);
            RecyclerView rv = new RecyclerView(getActivity());
            rv.addItemDecoration(new DividerItemDecoration(getActivity(),DividerItemDecoration.VERTICAL_LIST));
            rv.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.HORIZONTAL_LIST));
            GridLayoutManager gridLM = new GridLayoutManager(getActivity(),1, LinearLayoutManager.HORIZONTAL,false);
            rv.setLayoutManager(gridLM);
            rv.setAdapter(myRVAdapter4DB);
            llBody.addView(rv,params);
        }

        return v;
    }

    public void setDayofweek(int dayofweek){
        this.dayofweek = dayofweek;
    }

    class MyAdapter extends BaseAdapter {

        public MyAdapter() {
        }
        @Override
        public int getCount() {
            return values.size();
        }

        @Override
        public Object getItem(int position) {
            return values.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            Times times =  values.get(position);
            if(convertView == null){
                holder = new MyAdapter.ViewHolder();
                convertView = View.inflate(getActivity(), R.layout.listview_item,null);
                holder.tv = (TextView) convertView.findViewById(R.id.tv_lv_data);
                holder.ID = (TextView) convertView.findViewById(R.id.tv_lv_id);
                convertView.setTag(holder);
            }else {
                holder = (MyAdapter.ViewHolder) convertView.getTag();
            }

            holder.tv.setText(times.getStartTime()+"-"+times.getEndTime());
            holder.ID.setText(times.getTimeID()+"");

            return convertView;
        }

        class ViewHolder{
            TextView tv;
            TextView ID;
        }
    }

    class MyRVAdapter extends RecyclerView.Adapter<MyRVAdapter.MyHolder>{
        @Override
        public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = View.inflate(getActivity(),R.layout.recycler_item,null);
            view.setMinimumWidth(width);
            return new MyHolder(view);
        }

        @Override
        public void onBindViewHolder(final MyHolder holder, int position) {
            holder.textView.setText(sentryDBs.get(position).getSentry_name());
        }

        @Override
        public int getItemCount() {
            return sentryDBs.size();
        }

        class MyHolder extends RecyclerView.ViewHolder{

            TextView textView;
            LinearLayout linearLayout;
            MyHolder(View view) {
                super(view);
                textView = (TextView) view.findViewById(R.id.tv_lv_data);
                linearLayout = (LinearLayout) view.findViewById(R.id.ll_item);
            }
        }
    }

    class MyRVAdapter4DB extends RecyclerView.Adapter<MyRVAdapter4DB.MyHolder>{
        private List<Soldier> soldierDBs;

        public MyRVAdapter4DB(List<Soldier> soldierDBs) {
            this.soldierDBs = soldierDBs;
        }

        @Override
        public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item_db,null);
            parent.getLayoutParams().height = 64;
            view.setMinimumWidth(width);
            return new MyHolder(view);
        }

        @Override
        public void onBindViewHolder(final MyHolder holder, int position) {
            Soldier soldierDB = soldierDBs.get(position);
            if (null != soldierDB){
                holder.textView.setText(soldierDB.getSName());
            }else {
                holder.textView.setText("----");
            }
        }

        @Override
        public int getItemCount() {
            return soldierDBs.size();
        }

        class MyHolder extends RecyclerView.ViewHolder{
            TextView textView;
            LinearLayout ll;
            MyHolder(View view) {
                super(view);
                textView = (TextView) view.findViewById(R.id.tv_lv_data);
                ll = (LinearLayout) view.findViewById(R.id.ll_item);
            }
        }
    }
}
